package cn.seaboot.admin.core.web;

import cn.seaboot.admin.core.bean.DictionaryConfig;
import cn.seaboot.admin.core.service.DictionaryConfigService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.SystemConstant;
import cn.seaboot.admin.mvc.argument.ModifyArgument;
import cn.seaboot.admin.mvc.argument.Params;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 数据字典配置 [t_sys_dictionary_config] Controller
 *
 * @author Mr.css
 * @version 2021-12-09 14:53
 */
@Controller
@Tag(name = "数据字典配置 [t_sys_dictionary_config] 管理")
@RequestMapping("sys/dic/config")
public class DictionaryConfigCtrl {

    @Resource
    private DictionaryConfigService dicConfigService;

    /**
     * - 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated DictionaryConfig pojo) {
        return dicConfigService.insert(pojo);
    }

    /**
     * - 删
     *
     * @param tableName  表名
     * @param columnName 列名
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName,
            String tableName, String columnName) {
        return dicConfigService.deleteById(schemaName, tableName, columnName);
    }

    /**
     * - 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Validated DictionaryConfig pojo) {
        return dicConfigService.updateById(pojo);
    }

    /**
     * - 改
     *
     * @param arg 字段编辑
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 改")
    @RequestMapping(value = "edit", method = RequestMethod.PUT)
    public int updateInfo(@Validated ModifyArgument arg) {
        return dicConfigService.updateFieldById(arg);
    }

    /**
     * - 查
     *
     * @param tableName  表名
     * @param columnName 列名
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public DictionaryConfig queryById(
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName,
            @Params String tableName, String columnName) {
        return dicConfigService.queryById(schemaName, tableName, columnName);
    }

    /**
     * - 查列表
     *
     * @return PageInfo
     */
    @Operation(description = "数据字典配置 [t_sys_dictionary_config] 列表查询")
    @Parameters({
            @Parameter(name = "tableName", description = "表名")
    })
    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<DictionaryConfig> queryList(
            @RequestParam Map<String, Object> params,
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName) {
        params.put("schemaName", schemaName);
        return Pagination.apply(params, map-> dicConfigService.queryList(map));
    }
}
